package com.atguigu.utils.taskDisposeUtils;

import lombok.SneakyThrows;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

public class TaskDisposeUtils {

    @SneakyThrows
    public static <T> void send(List<T> coupons, Executor executor, Consumer<? super T> consumer) {

        //倒计时器
        CountDownLatch countDownLatch = new CountDownLatch(coupons.size());

        //模拟下发优惠券业务
        for (T coupon : coupons) {
            executor.execute(() -> {
                try {
                    consumer.accept(coupon);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
    }

    public static void disposeTask(String task) {
        System.out.println(String.format("【%s】disposeTask下发优惠卷或短信成功", task));
    }

    public static void disposeTaskV2(String task) {
        System.out.println(String.format("【%s】disposeTask下发邮件成功", task));
    }

    public static void disposeTaskV3(String task) {
        System.out.println(String.format("【%s】disposeTask下发注册码成功", task));
    }
}
